package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerCouponGet;
import com.huohuzhihui.merchant.exception.CouponCheckException;
import com.huohuzhihui.merchant.service.IMerCouponGetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 优惠券Controller
 * 
 * @author zylu
 * @date 2020-11-14
 */
@RestController
@RequestMapping("/api/coupon")
public class ApiCouponController extends BaseController
{
    @Autowired
    private IMerCouponGetService merCouponGetService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询我的优惠券
     * @param userId
     * @return
     */
    @RequestMapping("/findCouponListByUserId")
    @ResponseBody
    public AjaxResult findCouponListByUserId(Long userId){
        MerCouponGet merCouponGet = new MerCouponGet();
        merCouponGet.setUserId(userId);
        List<MerCouponGet> merCouponGetList = this.merCouponGetService.selectMerCouponGetList(merCouponGet);
        return AjaxResult.success("查询优惠券成功",merCouponGetList);
    }

    /**
     * 查询我的优惠券
     * @param
     * @return
     */
    @RequestMapping("/checkCoupon")
    @ResponseBody
    public AjaxResult checkCoupon(HttpServletRequest request,String couponCode){

        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            int result = this.merCouponGetService.checkCouponByCouponCode(couponCode);
            return AjaxResult.success("优惠券核销成功",result);
        }catch (CouponCheckException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询已核销的优惠券
     * @param
     * @return
     */
    @RequestMapping("/findCouponCheckList")
    @ResponseBody
    public AjaxResult findCouponCheckList(HttpServletRequest request){
        LoginUser loginUser =tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();

        MerCouponGet merCouponGet = new MerCouponGet();
        merCouponGet.setMerchantId(sysUser.getDeptId());
        merCouponGet.setUseStatus(1);
        List<MerCouponGet> merCouponGetList = this.merCouponGetService.selectMerCouponGetList(merCouponGet);
        return AjaxResult.success("查询优惠券成功",merCouponGetList);
    }

}
